//
// Created by 215609 on 2024/7/2.
//

#ifndef AUDIOCAMERA_TSAUDIO_H
#define AUDIOCAMERA_TSAUDIO_H

#include "TSPlayerStatus.h"

#include "TsCallJava.h"
#include "MNQueue.h"

extern "C" {
#include "libavcodec/avcodec.h"
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "libswresample/swresample.h"
}
class TSAudio {
public:
    AVCodecContext *avCodecContext = NULL;
    int duration = 0;
    AVCodecParameters *codecpar;

    pthread_t thread_play;
    int ret = 0;
    int streamIndex = -1;
    AVRational time_base;
    TSPlayerStatus *playstatus=NULL;
    int sample_rate = 0;
    uint8_t *buffer = NULL;

    AVPacket *avPacket = NULL;

    AVFrame *avFrame = NULL;

    double clock;//当前播放的时间    准确时间
    double last_tiem; //上一次调用时间
    // 引擎对象
    SLObjectItf engineObject = NULL;

    // 引擎接口
    SLEngineItf engineEngine = NULL;


    //混音器对象
    SLObjectItf outputMixObject = NULL;


    //混音器接口
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;


    //播放器对象
    SLObjectItf pcmPlayerObject = NULL;


    //播放器操作接口
    SLPlayItf pcmPlayerPlay = NULL;


    SLVolumeItf pcmVolumePlay = NULL;

//静音接口
    SLMuteSoloItf  pcmMutePlay = NULL;

    //缓冲器队列接口
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;

    TsCallJava *callJava = NULL;

    MNQueue *queue = NULL;
    int mute = 2;

    int data_size = 0;

    int now_time=0;
public:
    TSAudio(TSPlayerStatus *playstatus, int sample_rate,TsCallJava *callJava);

    ~TSAudio();

    void play();

    int resampleAudio();

    void initOpenSLES();

    int getCurrentSampleRateForOpensles(int sample_rate);

    void pause();

    void resume();

    void setVolume(int percent);

    void setMute(int mute);

    int getQueueSize();



};


#endif //AUDIOCAMERA_TSAUDIO_H
